﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;

namespace Data.Repositories
{
    public class RespostaRepository
    {
        ModeloCantinhoContainer db = new ModeloCantinhoContainer();

        public int GravaComentario(string texto, string parent,string email,int questionid)
        {
            UserRepository repUser = new UserRepository();
            User user = repUser.GetUserByEmail_Aprovado(email);
            int id = 0;
            if (parent == "0")
            {
                Answer a = new Answer
                {
                    DateCreated = DateTime.Now,
                    QuestionId = questionid,
                    Text = texto,
                    UserId = user.Id,
                    Votes = 0
                };
                //Grava pontos
                repUser.GravaPontos(PontosRepository.TipoPonto.Resposta,user);
                db.Answer.AddObject(a);
                SaveChanges();
                id = a.Id;
            }
            else
            {
                Comment c = new Comment {
                    Body = texto,
                    AnswerId = int.Parse(parent),
                    UserId = user.Id,
                    Data = DateTime.Now
                };
                //Grava pontos
                repUser.GravaPontos(PontosRepository.TipoPonto.Comentario, user);
                db.Comment.AddObject(c);
                SaveChanges();
                id = c.Id;
            }
            return id;
        }

        public Answer GetAnswer(int id)
        {
            return db.Answer.Include("User").Where(x => x.Id == id).SingleOrDefault();
        }

        public int Votar(int id, int accao,string email)
        {
            Answer a = GetAnswer(id);
            if (a == null) return -10000;
            
            UserRepository repUser = new UserRepository();

            if (a.UserId == repUser.GetUserByEmail_Aprovado(email).Id)
                return -10001;

            if (accao == 0) //Descer voto
            {
                a.Votes -= 1;
                //Grava pontos    
                repUser.GravaPontos(PontosRepository.TipoPonto.RespostaVotoNegativo, a.User);
            }
            else //Subir voto
            {
                a.Votes += 1;
                //Grava pontos 
                repUser.GravaPontos(PontosRepository.TipoPonto.RespostaVotoPositivo, a.User);
            }
            Update(a);

            return a.Votes;
        }


        public void Update(Answer answer)
        {
            try
            {
                db.Answer.Attach(answer);db.ObjectStateManager.ChangeObjectState(answer, System.Data.EntityState.Modified);
            }
            catch (Exception)
            {
                //controlar os erros
            }
            finally
            {
                
                SaveChanges();
            }

        }

        public void SaveChanges()
        {
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
